package com.db.util;

import java.sql.Connection;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


public class DBConnector {
	
	private static volatile DBConnector instance = null;
	
	@Resource(name="jdbc/mkyongdb")
	private DataSource ds;
   
	private DBConnector(){}

	public static DBConnector getInstance() {
		if (instance == null) {
			synchronized (DBConnector.class) {
				// Double check
				if (instance == null) {
					instance = new DBConnector();
				}
				
				try {
					Context ctx = new InitialContext();
					instance.ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mkyongdb");
				} catch (NamingException e) {
					e.printStackTrace();
				}
				
			}
		}
		return instance;
	}

	
	public Connection getConnection() throws SQLException{
		System.out.println("-------------- Receive DB Connection --------------");
		return ds.getConnection();
	}
}
